package com.bdth.weget.net.controller;

import com.bdth.weget.common.MessageResult;
import com.bdth.weget.net.base.SessionData;

import com.bdth.weget.net.bean.ChannelStatus;
import com.bdth.weget.net.vo.DataGridData;
import io.netty.channel.ChannelHandlerContext;
import org.hyperic.sigar.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Administrator on 2019/6/3.
 */
@Controller
public class HomeController {

    @RequestMapping("/sys/list")
    @ResponseBody
    public List<String> getLink(){
        List<String> link = new ArrayList<>();
        for (Map.Entry<ChannelHandlerContext,ChannelStatus> entry : SessionData.ALLLINK.entrySet()) {
            ChannelStatus cs = entry.getValue();
            link.add(cs.toString());
        }
        return link;
    }

    @RequestMapping("/sys/mem")
    @ResponseBody
    public Map<String, Object> getMem() throws SigarException {
        Sigar sigar = new Sigar();
        Mem mem = sigar.getMem();
        Map<String, Object> result = new HashMap<>();
        result.put("memory.total",mem.getTotal() / (1024 * 1024L));
        result.put("memory.used", mem.getUsed() / (1024 * 1024L));
        result.put("memory.free", mem.getFree() / (1024 * 1024L));
        return result;
    }

    @RequestMapping("/sys/cpu")
    @ResponseBody
    public Map<String, Object> getCpu() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[] = sigar.getCpuPercList();
        Map<String, Object> result = new HashMap<>();
        for (int i = 0, len = infos.length; i < len; i++) {// 不管是单块CPU还是多CPU都适用
            Map<String, Object> temp = new HashMap<>();
            CpuInfo info = infos[i];
            temp.put("mhz", info.getMhz()); //CPU的总量MHz
            temp.put("company", info.getVendor()); //CPU的厂商
            temp.put("model", info.getModel()); //CPU型号类别
            temp.put("cache.size", info.getCacheSize()); // 缓冲缓存数量
            CpuPerc cpu = cpuList[i];
            temp.put("freq.user", CpuPerc.format(cpu.getUser())); //CPU的用户使用率
            temp.put("freq.sys", CpuPerc.format(cpu.getSys())); //CPU的系统使用率
            temp.put("freq.wait", CpuPerc.format(cpu.getWait())); //CPU的当前等待率
            temp.put("freq.nice", CpuPerc.format(cpu.getNice())); //CPU的当前错误率
            temp.put("freq.idle", CpuPerc.format(cpu.getIdle())); //CPU的当前空闲率
            temp.put("freq.combined", CpuPerc.format(cpu.getCombined())); //CPU总的使用率
            result.put("cpu"+i,temp);
        }
        return result;
    }

    @RequestMapping(value = "/sys/jvm")
    @ResponseBody
    public Map<String, Object> getJvm() {
        Runtime r = Runtime.getRuntime();
        Properties props = System.getProperties();
        Map<String, Object> result = new HashMap<>();
        result.put("jvm.memory.total", r.totalMemory()); //JVM可以使用的总内存
        result.put("jvm.memory.free", r.freeMemory()); //JVM可以使用的剩余内存
        result.put("jvm.processor.avaliable", r.availableProcessors()); //JVM可以使用的处理器个数
        result.put("jvm.java.version", props.getProperty("java.version")); //Java的运行环境版本
        result.put("jvm.java.vendor", props.getProperty("java.vendor")); //Java的运行环境供应商
        result.put("jvm.java.home", props.getProperty("java.home")); //Java的安装路径
        result.put("jvm.java.specification.version", props.getProperty("java.specification.version")); //Java运行时环境规范版本
        result.put("jvm.java.class.path", props.getProperty("java.class.path")); //Java的类路径
        result.put("jvm.java.library.path", props.getProperty("java.library.path")); //Java加载库时搜索的路径列表
        result.put("jvm.java.io.tmpdir", props.getProperty("java.io.tmpdir")); //默认的临时文件路径
        result.put("jvm.java.ext.dirs", props.getProperty("java.ext.dirs")); //扩展目录的路径
        return result;
    }


    @RequestMapping("/sys/message")
    @ResponseBody
    public DataGridData<Map<String,String>> getMessage(){
        //超过20条删除
        while(SessionData.MESSAGELIST.size()>21){
            SessionData.MESSAGELIST.poll();
        }
        Iterator<Map<String,String>> iterator = SessionData.MESSAGELIST.iterator();
        List<Map<String,String>> list = new ArrayList<>();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        DataGridData<Map<String,String>> result = new DataGridData<Map<String,String>>(list.size(),list);
        return result;
    }

    @RequestMapping("/")
    public String index(){
        System.out.println(111);
        return "sys/index";
    }

}
